package com.xingchi.tornado.storage.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xingchi.tornado.basic.ErrorCode;
import com.xingchi.tornado.exception.BizException;
import com.xingchi.tornado.mybatisplus.service.AbstractBasePlusService;
import com.xingchi.tornado.security.context.RequestContext;
import com.xingchi.tornado.storage.core.mapper.UserFileRelMapper;
import com.xingchi.tornado.storage.core.model.domain.FileUserRelDO;
import com.xingchi.tornado.storage.core.model.dto.FileDTO;
import com.xingchi.tornado.storage.core.model.dto.FileUserRelDTO;
import com.xingchi.tornado.storage.core.model.dto.FileUserRelQueryDTO;
import com.xingchi.tornado.storage.core.service.FileService;
import com.xingchi.tornado.storage.core.service.FileUserRelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 用户文件关联表 服务实现类
 * </p>
 *
 * @author xingchi
 * @date 2024-05-04 20:02
 */
@Service
public class FileUserRelServiceImpl extends AbstractBasePlusService<UserFileRelMapper, FileUserRelDTO, FileUserRelQueryDTO, FileUserRelDO> implements FileUserRelService {

    @Autowired
    private FileService fileService;

    @Override
    public Wrapper<FileUserRelDO> getWrapper(FileUserRelQueryDTO query) {
        return Wrappers.<FileUserRelDO>lambdaQuery()
                .eq(StringUtils.isNotBlank(query.getSha256()), FileUserRelDO::getSha256, query.getSha256())
                .eq(StringUtils.isNotBlank(query.getPath()), FileUserRelDO::getPath, query.getPath())
                .eq(Objects.nonNull(query.getUserId()), FileUserRelDO::getUserId, query.getUserId())
                .eq(Objects.nonNull(query.getFileId()), FileUserRelDO::getFileId, query.getFileId());
    }

    @Override
    public boolean exists(String sha256) {

        if (StringUtils.isBlank(sha256)) {
            return false;
        }

        FileUserRelDTO fileUserRelDTO = this.selectOne(FileUserRelQueryDTO.builder().sha256(sha256).build());
        return fileUserRelDTO != null;
    }

    @Override
    public FileUserRelDTO rebuildOrGetUserFileRelDTO(String sha256) {

        FileDTO fileDTO = fileService.selectBySha256(sha256);
        BizException.throwIf(fileDTO == null, ErrorCode.FILE_NOT_FOUND, "文件不存在");

        // 查询是否存在文件关联信息
        FileUserRelDTO fileUserRelDTO = this.selectOne(FileUserRelQueryDTO.builder().sha256(sha256).build());
        if (fileUserRelDTO != null) {
            return fileUserRelDTO;
        }

        // 构建UserFileRelDTO
        fileUserRelDTO = FileUserRelDTO.builder()
                .name(fileDTO.getName())
                .path(fileDTO.getPath())
                .size(fileDTO.getSize())
                .suffix(fileDTO.getSuffix())
                .type(fileDTO.getType())
                .sha256(fileDTO.getSha256())
                .fileId(fileDTO.getId())
                .userId(Objects.nonNull(RequestContext.getUserId()) ? RequestContext.getUserId() : -1L)
                .build();

        this.insert(fileUserRelDTO);
        return fileUserRelDTO;
    }
}
